/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package am.aua.renju.bussinesslayer.gamelogic;

/**
 *
 * @author ara
 */
public class Checker {

    private Checker() {}

    public static boolean isDraw(BoardPositionState[][] board) {
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                if (board[i][j] == BoardPositionState.EMPTY) {
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean isVictory(BoardPositionState[][] board, boolean isWhite, int x, int y) {
        return horizontalCheck(board, isWhite, x, y) | verticalCheck(board, isWhite, x, y)
                | diagonal1Check(board, isWhite, x, y) | diagonal2Check(board, isWhite, x, y);
    }

    private static boolean horizontalCheck(BoardPositionState[][] board, boolean isWhite, int x, int y) {
        int tx = x;
        int victoryCount = 0;
        int i;

        for (i = 0; i < 4; i++) {
            if (tx != 14) {
                tx++;
                if (isWhite) {
                    if (board[tx][y] == BoardPositionState.WHITE) {
                        victoryCount++;
                    } else {
                        break;
                    }
                } else {
                    if (board[tx][y] == BoardPositionState.BLACK) {
                        victoryCount++;
                    } else {
                        break;
                    }
                }
            }
        }
        tx = x;
        int victoryCountLeft = 4 - victoryCount;
        for (i = 0; i < victoryCountLeft; i++) {
            if (tx != 0) {
                tx--;
                if (isWhite) {
                    if (board[tx][y] == BoardPositionState.WHITE) {
                        victoryCount++;
                    } else {
                        break;
                    }
                } else {
                    if (board[tx][y] == BoardPositionState.BLACK) {
                        victoryCount++;
                    } else {
                        break;
                    }
                }
            }
        }
        if (victoryCount == 4) {
            return true;
        }
        return false;
    }

    private static boolean verticalCheck(BoardPositionState[][] board, boolean isWhite, int x, int y) {
        //vertical 
        int victoryCount = 0;
        int i;
        int ty = y;
        for (i = 0; i < 4; i++) {
            if (ty != 14) {
                ty++;
                if (isWhite) {
                    if (board[x][ty] == BoardPositionState.WHITE) {
                        victoryCount++;
                    } else {
                        break;
                    }
                } else {
                    if (board[x][ty] == BoardPositionState.BLACK) {
                        victoryCount++;
                    } else {
                        break;
                    }
                }
            }
        }
        ty = y;
        int victoryCountLeft = 4 - victoryCount;
        for (i = 0; i < victoryCountLeft; i++) {
            if (ty != 0) {
                ty--;
                if (isWhite) {
                    if (board[x][ty] == BoardPositionState.WHITE) {
                        victoryCount++;
                    } else {
                        break;
                    }
                } else {
                    if (board[x][ty] == BoardPositionState.BLACK) {
                        victoryCount++;
                    } else {
                        break;
                    }
                }
            }
        }
        if (victoryCount == 4) {
            return true;
        }
        return false;

    }

    private static boolean diagonal1Check(BoardPositionState[][] board, boolean isWhite, int x, int y) {
        //diagonal 1
        int victoryCount = 0;
        int i;
        int tx = x, ty = y;
        for (i = 0; i < 4; i++) {
            if (ty != 14 && tx != 14) {
                ty++;
                tx++;
                if (isWhite) {
                    if (board[tx][ty] == BoardPositionState.WHITE) {
                        victoryCount++;
                    } else {
                        break;
                    }
                } else {
                    if (board[tx][ty] == BoardPositionState.BLACK) {
                        victoryCount++;
                    } else {
                        break;
                    }
                }
            }
        }
        ty = y;
        tx = x;
        int victoryCountLeft = 4 - victoryCount;
        for (i = 0; i < victoryCountLeft; i++) {
            if (tx != 0 && ty != 0) {
                tx--;
                ty--;
                if (isWhite) {
                    if (board[tx][ty] == BoardPositionState.WHITE) {
                        victoryCount++;
                    } else {
                        break;
                    }
                } else {
                    if (board[tx][ty] == BoardPositionState.BLACK) {
                        victoryCount++;
                    } else {
                        break;
                    }
                }
            }
        }
        if (victoryCount == 4) {
            return true;
        }
        return false;

    }

    private static boolean diagonal2Check(BoardPositionState[][] board, boolean isWhite, int x, int y) {
        //diagonal 2
        int victoryCount = 0;
        int i;
        int tx = x, ty = y;
        for (i = 0; i < 4; i++) {
            if (ty != 0 && tx != 14) {
                ty--;
                tx++;
                if (isWhite) {
                    if (board[tx][ty] == BoardPositionState.WHITE) {
                        victoryCount++;
                    } else {
                        break;
                    }
                } else {
                    if (board[tx][ty] == BoardPositionState.BLACK) {
                        victoryCount++;
                    } else {
                        break;
                    }
                }
            }
        }
        ty = y;
        tx = x;
        int victoryCountLeft = 4 - victoryCount;
        for (i = 0; i < victoryCountLeft; i++) {
            if (ty != 14 && tx != 0) {
                tx--;
                ty++;
                if (isWhite) {
                    if (board[tx][ty] == BoardPositionState.WHITE) {
                        victoryCount++;
                    } else {
                        break;
                    }
                } else {
                    if (board[tx][ty] == BoardPositionState.BLACK) {
                        victoryCount++;
                    } else {
                        break;
                    }
                }
            }
        }
        if (victoryCount == 4) {
            return true;
        }
        return false;

    }
}
